package converter;

import java.awt.geom.Area;
import java.awt.geom.Path2D;
import java.awt.geom.PathIterator;
import java.util.ArrayList;
import processing.core.PVector;

import com.sun.j3d.utils.geometry.GeometryInfo;

public class PolygonConverter {

	public Area createArea(ArrayList<PVector> points) {
		Path2D.Float path = new Path2D.Float();
		for (int i = 0; i < points.size(); i++) {
			PVector p = points.get(i);
			if (i == 0) {
				path.moveTo(p.x, p.y);
			} else {
				path.lineTo(p.x, p.y);
			}
		}
		path.closePath();
		return new Area(path);
	}
	public static Area getRect(float w,float h){
		float wh=w/2f;
		float hh=h/2f;
		Path2D.Float path = new Path2D.Float();
		path.moveTo(-wh, -hh);
		path.lineTo(wh, -hh);
		path.lineTo(wh, hh);
		path.lineTo(-wh, hh);
		path.closePath();
		return new Area(path);
	}
	public static Area getRect(float x1,float y1,float x2,float y2){
		Path2D.Float path = new Path2D.Float();
		path.moveTo(x1, y1);
		path.lineTo(x2, y1);
		path.lineTo(x2, y2);
		path.lineTo(x1, y2);
		path.closePath();
		return new Area(path);
	}
	public static GeometryInfo getGeometryInfo(ArrayList<ArrayList<PVector>> rings) {
		int[] stripCountArray = new int[rings.size()];
		int nNodes=0;
		for (int i = 0; i < rings.size(); i++) {
			ArrayList<PVector> ring = rings.get(i);
			stripCountArray[i] = ring.size();
			nNodes+=ring.size();
		}
		float[] coordinates = new float[nNodes * 3];
		int index=0;
		for (int i = 0; i < rings.size(); i++) {
			ArrayList<PVector> ring = rings.get(i);
			stripCountArray[i] = ring.size();
			for (int j = 0; j < ring.size(); j++) {
				PVector v=ring.get(j);
				coordinates[index] = v.x;
				index++;
				coordinates[index] = v.y;
				index++;
				coordinates[index] = v.z;
				index++;
			}
		}
		GeometryInfo gi = new GeometryInfo(GeometryInfo.POLYGON_ARRAY);
		gi.setCoordinates(coordinates);
		int[] counturCountArray = { stripCountArray.length };
		gi.setContourCounts(counturCountArray);
		gi.setStripCounts(stripCountArray);
		return gi;
	}

	public static  ArrayList<ArrayList<PVector>> getPoints(Area area) {
		PathIterator pi = area.getPathIterator(null);
		ArrayList<ArrayList<PVector>> allPoints = new ArrayList<ArrayList<PVector>>();
		ArrayList<PVector> points = new ArrayList<PVector>();
		while (pi.isDone() == false) {
			double[] coordinates = new double[6];
			int type = pi.currentSegment(coordinates);
			switch (type) {
			case PathIterator.SEG_MOVETO:
				points = new ArrayList<PVector>();
				points.add(new PVector((float) coordinates[0],
						(float) coordinates[1]));
				break;
			case PathIterator.SEG_LINETO:
				PVector newP = new PVector((float) coordinates[0],
						(float) coordinates[1]);
				if (!points.get(0).equals(newP)) {
					points.add(newP);
				}
				break;
			case PathIterator.SEG_CLOSE:
				allPoints.add(points);
				break;
			default:
				break;
			}
			pi.next();
		}
		return allPoints;
	}

	public static ArrayList<PVector> getProfile(float centerX, float centerY,
			float radius, int nSegments) {
		ArrayList<PVector> points = new ArrayList<PVector>();
		float dAngle = (float)(2f * Math.PI / nSegments);
		for (int i = 0; i < nSegments; i++) {
			float angle = dAngle * i;
			float x = centerX + radius * (float)(Math.cos(angle));
			float y = centerY + radius * (float)(Math.sin(angle));
			points.add(new PVector(x, y, 0));
		}
		return points;
	}
}